﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UI_LocationSelect : MonoBehaviour
{
    private bool m_bLoaded = false;
    private Dictionary<int, UI_Location_Title> m_dicAllLocationCell = new Dictionary<int, UI_Location_Title>();
    // Start is called before the first frame update

    public void Awake()
    {
        Load();
    }

    private void Load()
    {
        if (m_bLoaded)
        {
            return;
        }
        if (m_dicAllLocationCell.Count == 0)
        {
            UI_Location_Title[] s = GetComponentsInChildren<UI_Location_Title>();
            foreach (var item in s)
            {
                string objName = item.gameObject.name;
                string strID = objName.Substring(9, objName.Length - 9);
                int iID = int.Parse(strID);
                
                if (!m_dicAllLocationCell.ContainsKey(iID))
                {
                    m_dicAllLocationCell.Add(iID, item);
                    item.SetCallback(() => OnLocationClicked(iID));
                }
            }
        }
        m_bLoaded = true;
    }



    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void SetWarehouse(FWarehouse warehouse)
    {
        //foreach (var item in m_dicAllLocationCell)
        //{
        //    FLocation location = warehouse.GetLocationWithID(item.Key);
        //    if (location != null)
        //    {
        //        item.Value.SetLocation(location);
        //        int iID = location.ID;
        //        item.Value.SetCallback(() => OnLocationClicked(iID));
        //    }

        //}
    }

    void OnLocationClicked(int iID)
    {
        GlobalEvent.DispatchEvent(EventName.LocationSelect, new Event_LocationSelect() { LocationID = iID });
    }
}
